﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.Collections.Specialized;

namespace RoadTeam.Model.Base
{
    public class iEntityBasePub
    {
        /// <summary>
        /// 获取表名称以及所有的列名
        /// </summary>
        /// <param name="T">实体类的类型用typeof获取</param>
        /// <param name="_TableName">获取实体类对应的表名称</param>
        /// <param name="Columns">获取到对应的列属性</param>
        public static void GetEntityProperty(Type T, out TableAttribute _TableName, out ColumnAttribute[] Columns)
        {
            TableAttribute TableName = null;
            object[] tbobject = T.GetCustomAttributes(true);
            foreach (object obj in tbobject)
            {
                TableName = obj as TableAttribute;
                if (TableName != null) break;
            }
            _TableName = TableName;
            List<ColumnAttribute> LiColumn = new List<ColumnAttribute>();

            object[] objs;
            foreach (PropertyInfo Property in T.GetProperties())
            {
                objs = Property.GetCustomAttributes(true);
                foreach (object obj in objs)
                {
                    ColumnAttribute columnitem = obj as ColumnAttribute;
                    if (columnitem != null)
                    {
                        LiColumn.Add(columnitem);
                    }
                }
            }
            Columns = LiColumn.ToArray();
        }


        public static NameValueCollection GetPropertyValue(object O)
        {
            NameValueCollection NameValue = new NameValueCollection();
            NameValueCollection NameValueCollect;
            String StrValue = String.Empty;
            foreach (PropertyInfo P in O.GetType().GetProperties())
            {
                NameValueCollect = new NameValueCollection();
                if (P.GetValue(O, null) != null)
                {
                    StrValue = P.GetValue(O, null).ToString();
                }
                NameValueCollect.Set(P.Name, StrValue);
                NameValue.Add(NameValueCollect);
            }
            return NameValue;
        }



        public static NameValueCollection GetPropertyValue(NameValueCollection NameValue1, NameValueCollection NameValue2)
        {
            NameValueCollection NameValueCollect;
            for (int i = 0; i < NameValue1.Count; i++)
            {
                NameValueCollect = new NameValueCollection();
                NameValueCollect.Set(NameValue1.GetKey(i), NameValue1.GetValues(NameValue1.GetKey(i))[0]);
                NameValue2.Add(NameValueCollect);
            }
            return NameValue2;
        }

        public static T ChangeType<T>(object Value)
        {
            if (Value is T)
                return (T)Value;
            else if (Value == DBNull.Value || Value == null)
            {
                if (typeof(T) == typeof(DateTime))
                {
                    //如果取日期类型的默认值 0001/01/01 ,在WCF JSON序列化的时候，会失败。
                    object o = new DateTime(1900, 1, 1);
                    return (T)o;
                }
                else
                    return default(T);
            }
            else
            {
                //如果 Value为 decimal类型，Ｔ　为double 类型， (T)Value 将发生错误
                return (T)Convert.ChangeType(Value, typeof(T));
            }
        }



        public static void getParmValue(SqlWhere where, iTableEntityBase iInfo, ColumnAttribute[] Columns, ref NameValueCollection iCmdParam, ref String iSqlWhere)
        {
            if (iInfo != null)
            {
                String Temp = String.Empty;
                Type t = iInfo.GetType();
                PropertyInfo[] properties = t.GetProperties();
                for (int i = 0; i < Columns.Length; i++)
                {
                    //如果对象没有变化的,跳过!
                    if (!iInfo.PropertyChangedList.Contains(Columns[i].ColumnName)) continue;
                    object Tem = t.GetProperty(Columns[i].ColumnName).GetValue(iInfo, null);
                    //object Tem = t.InvokeMember(Columns[i].ColumnName, BindingFlags.GetProperty, null, iInfo, null);
                    Temp = (Tem == null) ? "" : Tem.ToString();
                    if (String.IsNullOrEmpty(Temp)) continue;
                    #region 这里需要添加对多种数据类型的最小值进行比较,相同的跳过
                    if (Temp == DateTime.MinValue.ToString()) continue;
                    #endregion
                    iSqlWhere += " AND  [" + Columns[i].ColumnName + "]=@" + Columns[i].ColumnName;
                    iCmdParam.Add("@" + Columns[i].ColumnName, Temp);
                }
            }
            #region 叠加后续条件
            if (!String.IsNullOrEmpty(where.StrSqlWhere))
            {
                iSqlWhere += " AND  (" + where.StrSqlWhere + ")";
            }

            if (where.PreSqlWhere != null && where.PreSqlWhere.Count > 0)
            {
                for (int i = 0; i < where.PreSqlWhere.Count; i++)
                {
                    iCmdParam.Add(where.PreSqlWhere.GetKey(i).ToString(), where.PreSqlWhere.GetValues(i)[0].ToString());
                }
            }
            #endregion
        }




    }
}
